home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: Bradd W. Szonye <bradds@ix.netcom.com>
- Newsgroups: comp.lang.c++
- Subject: RE: int::~int()
- Date: 20 Apr 1996 16:45:21 GMT
- Organization: Netcom
- Message-ID: <01bb2ed9.363f0020$65c2b7c7@Zany.localhost>
- References: <199604160100.KAA06143@public1.guangzhou.gd.cn>
- NNTP-Posting-Host: det-mi3-05.ix.netcom.com
- X-NETCOM-Date: Sat Apr 20 11:45:21 AM CDT 1996
- X-Newsreader: Microsoft Internet News
-
-
- On Monday, April 15, 1996, Wang TianXing wrote...
- > On Sun, 14 Apr 1996 13:49:59 +0900, Xu Yifeng <jafd@public.sta.net.cn>
- > wrote:
- >
- > | Hi everybody,
- >
- > | can your compiler compile following program?
- >
- > | //------------------------
- > | void main()
- > | {
- > | int i = 0;
- >
- > | i.int::~int();
- > | }
- > | //------------------------
- >
- > | is it a legal C++ program?
- >
- > Though many guys had said it is a legal one, I don't think it's a
- > meaningful one. The Sep, 95 draft Standard says in 12.4:
- >
- > 12 Invocation of destructors is subject to the usual rules for
- > member functions (_class.mfct_), e.g., an object of the
- > appropriate type is required (except invoking delete on a
- > null pointer has no effect). Once a destructor is invoked for an
- > object, the object no longer exists; the behavior is
- > undefined if the destructor is invoked for an object whose
- > lifetime has ended (_basic.life_). [Example: if the
- > destructor for an automatic object is explicitly invoked,
- > and the block is subsequently left in a manner that would
- > ordinarily invoke implicit destruction of the object, the
- > behavior is undefined. ]
- >
- > The [Example:...], though it isn't part of the standard, says
- > something directly related to your program.
- >
- > Although there're some differences between "well-formed program" and
- > "undefined behavior", I don't think it's a bad idea that a compiler
- > rejects this sort of code, as it will surely cause undefined
- > behaviors.
- >
- > The plain old Borland C++ 3.1 compiles
- >
- > int i = 0;
- > (&i)->int::~int();
- >
- > just fine. And that's the "Standard" way(via pointer) to explicitly
- > call a destructor.
-
- The example is contrived; you wouldn't ever want to call a destructor like
- that for an automatic object. You might want to do something like that,
- though, if you were dealing with a reference to a heap object in a
- template class. For another contrived example:
-
- template <class T> class Foo {
- public:
- Foo(T* bar) { Destroy(*bar); }
- void Destroy(T& bar) { bar.T::~T(); }
- }
-
- void Bar()
- {
- char* s = new char[sizeof(int)];
- Foo<int> test((int*)s);
- }
-
- That's the kind of circumstance that happens in the STL; you want the
- "destroy" function to invoke a destructor on raw memory, regardless of
- what type the destroyed object is.
-
-
-
-